package com.xxxx.crm.controller;

import com.xxxx.crm.base.BaseController;
import com.xxxx.crm.service.PermissionService;
import com.xxxx.crm.service.UserService;
import com.xxxx.crm.utils.LoginUserUtil;
import com.xxxx.crm.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author:王亚栋
 * @Date:2022/12/4 10:27
 * @Version:1.0
 */
@Controller
public class IndexController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 系统登录页
     * @return
     */
    @GetMapping("/index")
    public String index(){

        return "index";
    }

    /**
     * 系统欢迎页
     * @return
     */
    @GetMapping("/welcome")
    public String welcome(){

        return "welcome";
    }

    /**
     * 后端管理主页
     * @return
     */
    @GetMapping("/main")
    public String main(HttpServletRequest request){

        //查询登录用户信息

        //获取cookie中userId的值
        Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);

        //通过主键id查询用户信息
        User user = userService.selectByPrimaryKey(userId);

        //设置到session作用域中
        request.getSession().setAttribute("user",user);

        //通过当前登录用户ID查询当前登录用户拥有的资源列表(查询对应资源的授权码)
        List<String> permissions = permissionService.queryUserHasRoleHasPermissionByUserId(userId);

        //将集合设置到作用域中
        request.getSession().setAttribute("permissions",permissions);


        return "main";
    }
}
